package se.peterbjorkman.android.trafikkamera;

import se.peterbjorkman.android.trafikkamera.providers.CameraContentProvider;
import se.peterbjorkman.android.trafikkamera.providers.CameraDefs;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;

public class CountyCameraActivity extends CameraTreeActivity {
	
	private static final int CONTEXTMENU_ADDTOROUTE_ID =  Menu.FIRST;
	
	private static final int AddToRouteRequestCode = 1;
	
	@Override
    protected String[] getRootNames() {
		return new String[] { CameraDefs.NAME};
	}

    @Override
	protected int[] getRootViesIds() {
		return new int[] { android.R.id.text1 };
	}
	
	@Override
	protected int getRootLayout() {
		return android.R.layout.simple_expandable_list_item_1;
	}

	@Override
	protected Cursor getRootCursor() {
		String[] projection = new String[] { 
				CameraDefs.ID, 
				CameraDefs.NAME}; //TODO Cameradef

		return managedQuery(
				CameraContentProvider.COUNTY_URI,
				projection, null, null, CameraDefs.NAME);
	}
	
	@Override
	protected String getChildFilter(Cursor rootCursor) {
		int countyId = rootCursor.getInt(rootCursor.getColumnIndexOrThrow(CameraDefs.ID)); // TODO CameraDefs
		return CameraDefs.COUNTY + " = " + countyId;
	}
	
	@Override
	protected String getChildOrder() {
		return CameraDefs.ROAD + ", " + CameraDefs.NAME;
	}
	
	@Override
    protected View getEmptyView(){
    	TextView view = new TextView(this);
    	view.setText(R.string.NoCamerasInfo);
    	return view;
    }
	
	protected void onCreateContextMenuChild(ContextMenu menu, long groupId, long id){
		super.onCreateContextMenuChild(menu, groupId, id);
		if (SelectRouteActivity.shouldBeShown(id, this))
			menu.add(ContextMenu.NONE, CONTEXTMENU_ADDTOROUTE_ID, ContextMenu.NONE, getString(R.string.AddToRoute) );
	}
	
	protected boolean onContextItemSelectedChild(int itemId, long groupId, long childId) {
		if (itemId == CONTEXTMENU_ADDTOROUTE_ID){
			addToRoute(childId);
			return true;
		}
		return super.onContextItemSelectedChild(itemId, groupId, childId);
	}
	
	// TODO dulicated from CameraPictureActivity
	private void addToRoute(long id) {
		Intent intent = new Intent(this, SelectRouteActivity.class);
		intent.putExtra(SelectRouteActivity.EXTRA_ID, id);
		startActivityForResult(intent, AddToRouteRequestCode);
	}

	// TODO dulicated from CameraPictureActivity
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent intent){
		super.onActivityResult(requestCode, resultCode, intent);
		if (resultCode == RESULT_CANCELED)
			return;
		
		switch (requestCode){
			case AddToRouteRequestCode:
				HandleAddToRoute(intent);
			break;
			default:
		}
	}

	// TODO dulicated from CameraPictureActivity
	private void HandleAddToRoute(Intent intent) {
		long routeId =  intent.getLongExtra(SelectRouteActivity.SELECTEDID, 0);
		long cameraId = intent.getLongExtra(SelectRouteActivity.EXTRA_ID,   0);
		
		ContentResolver cr = getContentResolver();

        ContentValues values = new ContentValues();
        values.put("cameraid",       cameraId); // TODO hardcoded column names
        values.put("routeid",        routeId);
        values.put("sortorder",      0); // TODO sortorder
        
        cr.insert(CameraContentProvider.CAMERAROUTES_URI, values); 
	}
}
